package com.ssm.redis;

import org.redisson.Redisson;
import org.redisson.RedissonRedLock;
import org.redisson.api.RLock;
import org.redisson.api.RedissonClient;
import org.redisson.config.Config;


public class RedissonRedLockTest {
    public static void main(String[] args) throws InterruptedException {
        Config config = new Config();
        config.useClusterServers()
                .addNodeAddress("redis://127.0.0.1:7001")
                .addNodeAddress("redis://127.0.0.1:7002")
                .addNodeAddress("redis://127.0.0.1:7003")
                .addNodeAddress("redis://127.0.0.1:7004")
                .addNodeAddress("redis://127.0.0.1:7005")
                .addNodeAddress("redis://127.0.0.1:7006");

        RedissonClient redisson = Redisson.create(config);
        //可以是不同的客户端
        RLock lock1 = redisson.getLock("Lock1");
        RLock lock2 = redisson.getLock("Lock2");
        RLock lock3 = redisson.getLock("Lock3");
        RedissonRedLock lock = new RedissonRedLock(lock1, lock2, lock3);
        // 同时加锁：IOck1 LOCk2 LOcK3
        /// 红锁在大部分节点上加锁成功就算成功。
        lock.lock();
        lock.unlock();
        redisson.shutdown();
    }
}